package web;

import jdk.nashorn.internal.ir.IfNode;
import pojo.User;
import pojo.UserInfo;
import service.UserInfoService;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
    private UserInfoService userInfoService = new UserInfoService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //处理POST请求的乱码问题
        request.setCharacterEncoding("utf-8");

        String username  = request.getParameter("username");
        String password = request.getParameter("pwd");
        String type = request.getParameter("type");
        String id = request.getParameter("id");
        String status = request.getParameter("status");

//        复用AdminServlet，如果id不为空，则证明是使用启用，禁用用户状态功能，否则是展示全部用户信息功能
        if (id != null){
            if (status.equals("1")){
//                禁用功能，修改该id用户状态为0
                userInfoService.updateStatus(Integer.valueOf(id),"0");
                System.out.println("");
            }else {
//                启用功能，修改该id用户状态为1
                userInfoService.updateStatus(Integer.valueOf(id), "1");
            }
        }

//        进行所有用户信息展示
        List<UserInfo> userInfos = userInfoService.selectAll();
        request.setAttribute("userInfo",userInfos);

        request.getRequestDispatcher("/admin.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
